package inits

import (
	"classtype/global/model"
	"github.com/redis/go-redis/v9"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func Init() {
	MysqlConn()
	MysqlTableAuth()
	RedisConn()
}

func RedisConn() {
	model.RDB = redis.NewClient(&redis.Options{
		Addr:     "14.103.158.217:6379",
		Password: "redis_x3jTtC",
		DB:       0,
	})
}

func MysqlTableAuth() {
	if err := model.DB.AutoMigrate(&model.ClassTypes{}); err != nil {
		panic(err)
	}
}

func MysqlConn() {
	var err error
	model.DB, err = gorm.Open(mysql.Open("root:89B158B4DAFE6F5461878D26EA49B100@tcp(14.103.158.217:3306)/8.28exam?charset=utf8&parseTime=True&loc=Local"))
	if err != nil {
		panic(err)
	}
	db, err := model.DB.DB()
	if err != nil {
		panic(err)
		return
	}
	db.SetMaxIdleConns(25)
	db.SetMaxOpenConns(100)
}
